package top.starp.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class CmdUtil {

    public static void main(String[] args) throws IOException {
//        String dir = CmdUtil.runCmd("dir");
//        System.out.println("dir");
//        System.out.println(dir);

        String repoUrl="https://gitee.com/shenliyang/git-report.git";
        String localDir="D:\\proj\\springBoot\\git-report";
        String cloneCmd = String.format("git clone %s %s", repoUrl, localDir);
        System.out.println("cloneCmd");
        System.out.println(cloneCmd);
        String res = CmdUtil.runCmd(cloneCmd);
        System.out.println("res");
        System.out.println(res);
    }

   public static String downloadGitRepo(String repoUrl,String localDir) throws IOException {
        String cloneCmd = String.format("git clone %s %s", repoUrl, localDir);
//        String repoUrl="https://gitee.com/shenliyang/git-report.git";
//        String localDir="D:\\proj\\springBoot\\git-report";
//        String cloneCmd = String.format("git clone %s %s", repoUrl, localDir);
        System.out.println("cloneCmd");
        System.out.println(cloneCmd);
        String res = CmdUtil.runCmd(cloneCmd);
        System.out.println("res");
        System.out.println(res);
        return res;
    }

    public static void runCmdAndShow(String commandStr) throws IOException {
        Process exec = Runtime.getRuntime().exec(commandStr);
//        System.out.println("exec");
//        System.out.println(exec);
//        IOUtil.
        IoUtil.read(exec.getInputStream());
    }

    public static String runCmd(String commandStr) throws IOException {
        String trueCmdStr="cmd /c "+commandStr;
        Process exec = Runtime.getRuntime().exec(trueCmdStr);
//        Process exec = Runtime.getRuntime().exec(commandStr);
//        System.out.println("exec");
//        System.out.println(exec);
//        IOUtil.
        String res = IoUtil.readRet(exec.getInputStream());
        return  res;
    }
}
